﻿@model EntityField
@using NewLife;
@using NewLife.Cube;
@using Microsoft.AspNetCore.Routing
@using NewLife.Cube.Extensions;
@using XCode;
@using XCode.Configuration;
@{
    var entity = Model.Entity;
    var item = Model.Field;
    var factory = entity.GetType().AsFactory();

    var value = entity[item.Name];
    var provider = ManageProvider.Provider;
    var listField = item as ListField;

    // 第二名称，去掉后面的数字，便于模式匹配
    var digits = new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    var name2 = item.Name.TrimEnd(digits);
}
@if (item.PrimaryKey)
{
    <td class="text-center">@value</td>
}
else
{
    switch (Type.GetTypeCode(item.Type))
    {
        case TypeCode.Boolean:
            var bval = (Boolean)value;
            <td class="text-center">
                @if (item.Name.EqualIgnoreCase("Enable") && factory.Unique != null)
                {
                    var key = entity[factory.Unique.Name];
                    <a href="@Url.Action("SetEnable",null,new { id=key, enable=!bval })" data-action="action">
                        @await Html.PartialAsync("_List_Data_Item_IconBoolean", bval)
                    </a>
                }
                else
                {
                    @await Html.PartialAsync("_List_Data_Item_IconBoolean", bval)
                }
            </td>
            break;
        case TypeCode.DateTime:
            var dt = (DateTime)value;
            if (name2.EndsWith("Date"))
            {
                <td>@dt.ToString("yyyy-MM-dd", "")</td>
            }
            else
            {
                <td>@dt.ToFullString("")</td>
            }
            break;
        case TypeCode.Decimal:
            if (listField.GetValue != null)
            {
                <td class="text-end">@listField.GetValue(entity)</td>
            }
            else if (item.ItemType.EqualIgnoreCase("Money"))
            {
                var dc = (Decimal)value;
                <td class="text-end">@dc.ToString("c")</td>
            }
            else
            {
                var dc = (Decimal)value;
                <td class="text-end">@dc.ToString("n2")</td>
            }
            break;
        case TypeCode.Single:
        case TypeCode.Double:
            if (listField.GetValue != null)
            {
                <td class="text-end">@listField.GetValue(entity)</td>
            }
            else if(item.ItemType.EqualIgnoreCase("percent", "Percentage"))
            {
                var dd = value.ToDouble();
                <td class="text-end">@dd.ToString("p2")</td>
            }
            else if (item.ItemType.EqualIgnoreCase("percent", "Percentage"))
            {
                var dd = value.ToDouble();
                var des = item.Description + "";
                if (des.Contains("十分之一")) { dd /= 10; }
                if (des.Contains("百分之一")) { dd /= 100; }
                if (des.Contains("千分之一")) { dd /= 1000; }
                if (des.Contains("万分之一")) { dd /= 10000; }

                <td class="text-end">@dd.ToString("p2")</td>
            }
            else
            {
                var dd = value.ToDouble();
                <td class="text-end">@dd.ToString()</td>
            }
            break;
        case TypeCode.Byte:
        case TypeCode.Int16:
        case TypeCode.Int32:
        case TypeCode.Int64:
        case TypeCode.UInt16:
        case TypeCode.UInt32:
        case TypeCode.UInt64:
            //特殊处理枚举
            if (item.Type.IsEnum)
            {
                var val = "";
                try
                {
                    val = EnumHelper.GetDescription((Enum)value) ?? value + "";
                }
                catch { }
                <td>@val</td>
            }
            else if (item.ItemType.StartsWithIgnoreCase("TimeSpan"))
            {
                <td class="text-end">@(item.FormatValue(value))</td>
            }
            else if (item.ItemType.StartsWithIgnoreCase("Time"))
            {
                <td class="text-center">@(item.FormatValue(value))</td>
            }
            else if (item.ItemType.EqualIgnoreCase("GMK"))
            {
                <td class="text-end">@(value.ToLong().ToGMK())</td>
            }
            else if (item.Name.EqualIgnoreCase("CreateUserID", "UpdateUserID"))
            {
                //var provider = ViewData["Provider"] as IManageProvider;
                if (provider != null)
                {
                    <td class="text-center">@provider?.FindByID(value)</td>
                }
                else
                {
                    <td class="text-center">@value</td>
                }
            }
            else if (item.ItemType.EqualIgnoreCase("percent", "Percentage"))
            {
                var dd = Convert.ToDouble(value);
                var des = item.Description + "";
                if (des.Contains("十分之一")) { dd /= 10; }
                if (des.Contains("百分之一")) { dd /= 100; }
                if (des.Contains("千分之一")) { dd /= 1000; }
                if (des.Contains("万分之一")) { dd /= 10000; }

                <td class="text-end">@dd.ToString("p2")</td>
            }
            else
            {
                <td class="text-end">@Convert.ToInt64(value).ToString("n0")</td>
            }
            break;
        case TypeCode.String:
        default:
            {
                var val = value;
                if (!item.MapField.IsNullOrEmpty() && val + "" != "")
                {
                    if (item.MapProvider != null)
                    {
                        var name = item.MapProvider.EntityType.Name;
                        var key = item.MapProvider.Key;
                        val = entity[item.MapField];
                        <td><a href="@name?@key=@val">@value</a></td>
                    }
                    else
                    {
                        <td>@value</td>
                    }
                }
                else if (item.Name.EqualIgnoreCase("CreateIP", "UpdateIP"))
                {
                    <td class="text-center" title="@((value+"").IPToAddress())">@value</td>
                }
                else if(item.Name.EqualIgnoreCase("TraceId"))
                {
                    var traceId = (String)value;
                    if (!traceId.IsNullOrEmpty())
                    {
                        <td class="text-center align-middle">
                            <a href="@StarHelper.BuildUrl(traceId)" title="链路追踪，用于APM性能追踪定位，还原该事件的调用链" target="_blank">追踪</a>
                        </td>
                    }
                    else
                    {
                        <td>@value</td>    
                    }
                }
                else if (item.IsBigText())
                {
                    //防止内容过长
                    <td style="max-width:360px;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;" data-bs-toggle="tooltip" title="@value">@value</td>
                }
                else
                {
                    <td>@value</td>
                }
            }
            break;
    }
}
